package login.User;

import login.JDBCHelper;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDao {
    //登录
    public static User login(String name, String password) {
        User user=null;
        try {
            ResultSet rs= JDBCHelper.query("select * from t_user where name=? and password=?",name,password);
            if(rs!=null&&rs.next()){
                user=new User();
                user.setName(rs.getString("name"));
                user.setPassword(rs.getString("password"));
                user.setRealName(rs.getString("realName"));
                user.setBirthDate(rs.getString("birthDate"));
                user.setEmail(rs.getString("email"));
                user.setTelephone(rs.getString("telephone"));
                user.setAddress(rs.getString("address"));
                user.setPostcode(rs.getInt("postcode"));
                user.setPrivilege(rs.getString("privilege"));
            }
        } catch (SQLException e) {
            System.out.println("登陆出错"+e.getMessage());
        }
        finally {
            //JDBCHelper.close();
        }
        return user;
    }

    //注册
    public static void register(User user) throws SQLException {
        String sql="" +
                "insert into t_user"+
                "(name,password,realName,birthDate,email,telephone,address,postcode) "+
                "values("+
                "?,?,?,?,?,?,?,?)";
        JDBCHelper.create(user,sql);
    }
}